﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ORMAC;
using ORMAC.Core;
using System.Data;
using System.Data.SqlClient;
using ORMAC.Test;
using System.Diagnostics;
namespace ORMAC.UnitTest.Helper
{
    public class ADOHelper
    {
        private string connectionString;
        public ADOHelper(string connectionString){
            this.connectionString = connectionString;
        }
        public int GetTableRowCount<T>()
        {
            int res = 0;
            string tableName=TestHelper.TableName<T>();
            Stopwatch s = new Stopwatch();
            s.Start();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //Create connection to the DB
                using (SqlCommand searchCommand = new SqlCommand())
                {
                    connection.Open();
                    searchCommand.CommandType = CommandType.Text;

                    searchCommand.CommandText = "select count(*) from "+tableName;
                    searchCommand.Connection = connection;
                    res=(int)searchCommand.ExecuteScalar();
                }
            }
            s.Stop();
            Console.WriteLine("ADO Time:"+ s.ElapsedMilliseconds);
            return res;

        }

        internal int GetTableRows<T>()
        {
            int res = 0;
            string tableName = TestHelper.TableName<T>();
            Stopwatch s = new Stopwatch();
            s.Start();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                //Create connection to the DB
                using (SqlCommand searchCommand = new SqlCommand())
                {
                    connection.Open();
                    searchCommand.CommandType = CommandType.Text;

                    searchCommand.CommandText = "select * from " + tableName;
                    searchCommand.Connection = connection;
                    SqlDataReader r= searchCommand.ExecuteReader();
                    bool n = true;
                    res= 0;
                    while (r.Read())
                    {
                        res++;
                    }
                }
            }
            s.Stop();
            Console.WriteLine("ADO Time:" + s.ElapsedMilliseconds);
            return res;

        }
    }
}
